/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.services;

import com.kcp.ko.authentication.domain.AuthenticatableUser;
import com.kcp.ko.fmtnmlib.pdf.forms.PDFFormFiller;
import com.kcp.ko.fmtnmlib.pdf.forms.PDFTextField;

import java.io.ByteArrayOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Builds the certificate email for completion of the Tests.  Also calls upon
 * the PDFAnnotationFormFiller to fill out and "PDF" a FDF.
 * 
 * @author John Ericksen
 *
 */
public class CompletionCertificate {
    
    private String moduleName;
    private AuthenticatableUser user;
    
    public CompletionCertificate(String moduleName, AuthenticatableUser user){
        this.moduleName = moduleName;
        this.user = user;
        
    }

    public byte[] buildCertificate() throws Exception
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        
        PDFFormFiller.fillFormAndFlatten(out, this.getClass().getClassLoader().getResourceAsStream("certificate.pdf"), this);
        
        return out.toByteArray();
    }

    @PDFTextField("page[0].page[0].Module")
    public String getModuleName() {
        return moduleName;
    }
    
    @PDFTextField("page[0].page[0].Name")
    public String getName(){
        return this.user.getProfile().getName().toString();
    }
    
    @PDFTextField("page[0].page[0].Unumber")
    public String getUnumber(){
        return this.user.getProfile().getUsername();
    }
    
    @PDFTextField("page[0].page[0].Date")
    public String getDate(){
    	DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
        return df.format(new Date());
    }

}
